#!/usr/bin/python
# encoding=utf-8
# G
# cosine 整理

import re
import urllib2

def get_urls_dict(html,tags):
	"""调用get_att_value获取指定tag的链接"""
	urlsDict = {}
	tagNames = {}
	for tag in tags:
		if tag in ('a','area','link'):
			tagNames[tag] = 'href'
		elif tag in ('iframe','frame','script','img'):
			tagNames[tag] = 'src'
		elif tag in ('form'):
			tagNames[tag] = 'action'
		else:
			continue
	for tag in tagNames:
		name = tagNames[tag]
		urls = get_att_value(html,tag,name)
		urlsDict[tag] = urls
	return urlsDict

def get_att_value(html,tag,att_name):
	"""获取指定tag与属性名的值列表"""
	tag_start = '<' + tag
	if tag_start not in html:
		return []
	values = []
	tag_content_pattern = r"""<%s([^>]+)>"""%tag
	tag_content_reobj = re.compile(tag_content_pattern,  re.IGNORECASE)
	tag_contents = tag_content_reobj.findall(html)
	tag_value_pattern = r"""\b%s\s*=\s*(?:[\\]?"([^"]*?)[\\]?"|[\\]?'([^']*?)[\\]?'|([^>'"\s]+))"""%att_name
	tag_value_reobj = re.compile(tag_value_pattern,re.IGNORECASE)
	if tag_contents != []:
		for content in tag_contents:
			value = tag_value_reobj.findall(content)
			if value != []:
				for i in value[0]:
					if i != "" and i not in values:
						values.append(i)
		return values
	else:
		return []

def get_meta_url(html_data):
	"""获取meta链接"""
	if '<meta' not in html_data:
		return []
	rawstr = r"""<meta([^>]+)>"""
	compile_obj = re.compile(rawstr,  re.IGNORECASE)
	meta_tag_params = compile_obj.findall(html_data)
	rawstr = r"""\burl\s*=\s*(?:[\\]?"([^"]*?)[\\]?"|[\\]?'([^']*?)[\\]?'|([^>'"\s]+))"""
	compile_obj = re.compile(rawstr,re.IGNORECASE)
	meta_list = []
	if meta_tag_params != []:
		for one_params in meta_tag_params:
			meta_url = compile_obj.findall(one_params)
			if meta_url != []:
				for i in meta_url[0]:
					i = i.strip()
					if i != "" and i not in meta_list:
						meta_list.append(i)
		return meta_list
	else:
		return []

	
if __name__ == '__main__':
	import sys
	try:
		url = sys.argv[1]
	except:
		print 'Usage: python tagparse.py http://www.knownsec.com'
		sys.exit(0)
		
	u = urllib2.urlopen(url)
	print u.url
	html = u.read()
	u.close()
	print get_urls_dict(html,["a","area","iframe","frame","meta","script","form","img","style"])
	
